package com.log4jviewer.ui.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarPushButton;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotToolbarRadioButton;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;

import utils.swtbot.UITest;

/**
 * The class that incapsulates all necessary functionality for simple "Log4j" view UI tests creating.
 * 
 * @author <a href="mailto:Daniil.Yaroslavtsev@gmail.com">Daniil Yaroslavtsev</a>
 */
public abstract class AbstractMainViewTest extends UITest {

    /** The UI testing bot. */
    private static SWTWorkbenchBot bot = getBot();

    /** The "Open error view" button. */
    private static SWTBotToolbarPushButton openErrsViewButton;

    /** The "Load logs from text file" button. */
    private static SWTBotToolbarPushButton loadLogsForFileButton;

    /** The "Start Log4j Server" button. */
    private static SWTBotToolbarRadioButton startLog4jServerButton;

    /** The "Stop  Log4j Server" button. */
    private static SWTBotToolbarRadioButton stopLog4jServerButton;

    /** The "Clear log table" button. */
    private static SWTBotToolbarPushButton clearLogTableButton;

    @BeforeClass
    public static void beforeClass() {
        // BOT.resetWorkbench(); // set the workbench to it`s initial state
        setupSWTBotParams();

        closeUnnecessaryViewsIfNeeded(); // Close any windows and views except "Log4j-Viewer"
        findAndParsePageControls(); // and parse "Log4j" view controls.
    }

    @Before
    public void beforeTest() {
        setInitialViewState();
    }

    @After
    public void afterTest() {
        setInitialViewState();
    }

    @AfterClass
    public static void afterClass() {
        doSleepBetweenTests();
    }

    /**
     * Finds and parses the page controls.
     */
    protected static void findAndParsePageControls() {
        openErrsViewButton = getToolbarPushButtonWithTooltip("Open error view");
        loadLogsForFileButton = getToolbarPushButtonWithTooltip("Load logs from text file");
        startLog4jServerButton = getToolbarRadioButtonWithTooltip("Start Log4j Server");
        stopLog4jServerButton = getToolbarRadioButtonWithTooltip("Stop  Log4j Server");
        clearLogTableButton = getToolbarPushButtonWithTooltip("Clear log table");
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static SWTBotToolbarPushButton getToolbarPushButtonWithTooltip(final String toolTipText) {
        Matcher matcher = Matchers.allOf(
                WidgetMatcherFactory.widgetOfType(ToolItem.class),
                WidgetMatcherFactory.withTooltip(toolTipText),
                WidgetMatcherFactory.withStyle(SWT.PUSH, "push")
                );
        ToolItem toolItem = (ToolItem) bot.widget(matcher);
        return new SWTBotToolbarPushButton(toolItem);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static SWTBotToolbarRadioButton getToolbarRadioButtonWithTooltip(final String toolTipText) {
        Matcher matcher = Matchers.allOf(
                WidgetMatcherFactory.widgetOfType(ToolItem.class),
                WidgetMatcherFactory.withTooltip(toolTipText),
                WidgetMatcherFactory.withStyle(SWT.RADIO, "radio")
                );
        ToolItem toolItem = (ToolItem) bot.widget(matcher);
        return new SWTBotToolbarRadioButton(toolItem);
    }

    // Setters

    // Getters

    /**
     * Gets the "Open error view" button.
     * 
     * @return the "Open error view" button
     */
    protected static SWTBotToolbarPushButton getOpenErrsViewButton() {
        return openErrsViewButton;
    }

    /**
     * Gets the "Load logs from text file" button.
     * 
     * @return the "Load logs from text file" button
     */
    protected static SWTBotToolbarPushButton getLoadLogsForFileButton() {
        return loadLogsForFileButton;
    }

    /**
     * Gets the "Start  Log4j Server" button.
     * 
     * @return the "Start  Log4j Server" button
     */
    protected static SWTBotToolbarRadioButton getStartLog4jServerButton() {
        return startLog4jServerButton;
    }

    /**
     * Gets the "Stop  Log4j Server" button.
     * 
     * @return the "Stop  Log4j Server" button
     */
    protected static SWTBotToolbarRadioButton getStopLog4jServerButton() {
        return stopLog4jServerButton;
    }

    /**
     * Gets the "Clear log table" button.
     * 
     * @return the "Clear log table" button
     */
    protected static SWTBotToolbarPushButton getClearLogTableButton() {
        return clearLogTableButton;
    }

    // "press ... button" methods

    /**
     * Presses the "Open error view" button.
     */
    protected static void pressOpenErrsViewButton() {
        openErrsViewButton.click();
    }

    /**
     * Presses the "Load logs from text file" button.
     */
    protected static void pressLoadLogsForFileButton() {
        loadLogsForFileButton.click();
    }

    /**
     * Presses the "Start Log4j Server" button.
     */
    protected static void selectStartLog4jServerButton() {
        startLog4jServerButton.select();
    }

    /**
     * Presses the "Stop  Log4j Server" button.
     */
    protected static void selectStopLog4jServerButton() {
        stopLog4jServerButton.select();
    }

    /**
     * Presses the "Clear log table" button.
     */
    protected static void pressClearLogTableButton() {
        clearLogTableButton.click();
    }

    /**
     * Sets the initial view state.
     */
    protected static void setInitialViewState() {
        // no code
    }

}
